import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router/index'
// 创建axios实例
const service = axios.create({
    baseURL: 'http://47.109.26.88:8888',
    // baseURL: 'http://api.rcb.com',
    // baseURL: 'http://127.0.0.1:8090',
    timeout: 10000 // 请求超时时间
})
// http request 拦截器
service.interceptors.request.use(
    config => {
    //判断cookie中是否有值
    if(localStorage.getItem("token")){
        //将cookie中token的值放到headers中
        config.headers['token'] = localStorage.getItem("token")
    }
    return config
},
    err => {
    return Promise.reject(err)
})
// http response 拦截器
service.interceptors.response.use(
    response => {
        //根据状态码code判断是否登录
        if(response.data.code === 400012){
            //208表示未登录，弹出登录框，或者token过期了
            window.alert("登录已过期，请重新登录！")
            router.push('/')
            return
        }else{
            if (response.data.code !== 200) {
                Message({
                    message: response.data.message,
                    type: 'error',
                    duration: 5 * 1000
                })
                return Promise.reject(response)
            } else {
                return response
            }
        }
    },
    error => {
        return Promise.reject(error.response)
})
export default service

